﻿using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using nyxwin.Data.Base;
using nyxwin.Data.Utils;
using nyxwin.Data.Common;

namespace nyxwin.Data.Friends
{
    public class FriendManager : RemoteBase
    {
        #region Events

        public event EventHandler<GetFriendsEventArgs> GetActiveFriendsCompleted;
        public event EventHandler<GetFriendsEventArgs> GetFriendsCompleted;

        #endregion

        #region Construction

        public FriendManager(string authorizationNick, string authorizationToken)
            : base(authorizationNick, authorizationToken)
        {
            base.InvocationCompleted += FriendManagerInvocationCompleted;
        }

        #endregion

        #region GetActiveFriends

        public void GetActiveFriends()
        {
            var parameters = new Dictionary<string, string>();
            base.InvokeRemote("people", "active_friends", parameters.ToUrlArray());
        }

        #endregion

        #region GetFriends

        public void GetFriends()
        {
            var parameters = new Dictionary<string, string>();
            base.InvokeRemote("people", "friends", parameters.ToUrlArray());
        }

        #endregion

        #region FireGetActiveFriendsCompleted

        private void FireGetActiveFriendsCompleted(GetFriendsEventArgs e)
        {
            if (null != GetActiveFriendsCompleted)
            {
                GetActiveFriendsCompleted(this, e);
            }
        }

        #endregion

        #region FireGetFriendsCompleted

        private void FireGetFriendsCompleted(GetFriendsEventArgs e)
        {
            if (null != GetFriendsCompleted)
            {
                GetFriendsCompleted(this, e);
            }
        }

        #endregion

        #region FriendManagerInvocationCompleted

        private void FriendManagerInvocationCompleted(object sender, InvocationEventArgs e)
        {
            if (e.Method != "people")
                throw new NyxException(string.Format("Expected 'people', but got '{0}' instead", e.Method));

            var data = JsonConvert.DeserializeObject(e.Result) as JObject;

            switch (e.Parameter)
            {
                case "active_friends":
                    {
                        var dtStart = System.DateTime.Now;
                        var listFriends = new List<Friend>();

                        foreach (var row in data["data"])
                        {
                            var friend = new Friend();

                            friend.Nick = row.Value<string>("nick");
                            
                            if (row["active"] != null)
                                friend.Active = Active.ParseJson(row.Value<JObject>("active"), dtStart);
                            else
                                friend.Active = null;

                            listFriends.Add(friend);
                        }

                        FireGetActiveFriendsCompleted(new GetFriendsEventArgs { FriendList = listFriends });
                    }
                    break;

                case "friends":
                    {
                        var listFriends = new List<Friend>();
                        var dtStart = System.DateTime.Now;

                        foreach (JObject row in data["data"])
                        {
                            var friend = new Friend();

                            friend.Nick = row.Value<string>("nick");

                            if (row["active"] != null)
                                friend.Active = Active.ParseJson(row.Value<JObject>("active"), dtStart);
                            else
                                friend.Active = null;

                            listFriends.Add(friend);
                        }

                        FireGetFriendsCompleted(new GetFriendsEventArgs { FriendList = listFriends });
                    }
                    break;
            }
        }

        #endregion
    }
}